package com.mobilepay.moneypig.model.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.base.NoDoubleClickListener;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.activity.search.BankActivity;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;

/**
 * Created by xlm on 2017/7/29 0029
 */

public class AddCreditActivity extends BaseActivity {
    private static final String TAG = "AddCreditActivity";
    private RelativeLayout mRelativeBack;
    private Button mButtonCredit;
    private EditText mEditCDNum;
    private EditText mEditCDMobile;
    private EditText mEditName;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_add_credit);
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.add_back);
        mButtonCredit = (Button) findViewById(R.id.credit_add_confirm);
        mEditCDNum = (EditText) findViewById(R.id.credit_add_num);
        mEditCDMobile = (EditText) findViewById(R.id.credit_add_mobile);
        mEditName = (EditText) findViewById(R.id.credit_add_name);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_back:
                finish();
                break;
        }
    }

    private static final int REQUST_BANK = 1;
    String bankName;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            bankName = data.getStringExtra("name");
            mEditName.setText(bankName);
        }
    }

    @Override
    protected void onSetListener() {
        mEditName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getThis(), BankActivity.class);
                startActivityForResult(intent, REQUST_BANK);
            }
        });
        mRelativeBack.setOnClickListener(this);
        mButtonCredit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                super.onNoDoubleClick(view);
                bindCard();
            }
        });
//
//        mEditCDNum.addTextChangedListener(new TextWatcher() {
//
//            private int oldLength = 0;
//            private boolean isChange = true;
//            private int curLength = 0;
//            private int emptyNumB = 0;
//            private int emptyNumA = 0;
//
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                oldLength = s.length();
//                emptyNumB = 0;
//                for (int i = 0; i < s.toString().length(); i++) {
//                    if (s.charAt(i) == ' ') emptyNumB++;
//                }
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                curLength = s.length();
//                if (curLength == oldLength || curLength <= 3) {
//                    isChange = false;
//                } else {
//                    isChange = true;
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (isChange) {
//                    int selectIndex = mEditCDNum.getSelectionEnd();//获取光标位置
//                    String content = s.toString().replaceAll(" ", "");
//                    StringBuffer sb = new StringBuffer(content);
//                    //遍历加空格
//                    int index = 1;
//                    emptyNumA = 0;
//                    for (int i = 0; i < content.length(); i++) {
//                        if ((i + 1) % 4 == 0) {
//                            sb.insert(i + index, " ");
//                            index++;
//                            emptyNumA++;
//                        }
//                    }
//                    String result = sb.toString();
//                    if (result.endsWith(" ")) {
//                        result = result.substring(0, result.length() - 1);
//                    }
//                    mEditCDNum.setText(result);
//                    if (emptyNumA > emptyNumB)
//                        selectIndex = selectIndex + (emptyNumA - emptyNumB);
//                    //处理光标位置
//                    if (selectIndex > result.length() || selectIndex + 1 == result.length()) {
//                        selectIndex = result.length();
//                    } else if (selectIndex < 0) {
//                        selectIndex = 0;
//                    }
//                    mEditCDNum.setSelection(selectIndex);
//                    isChange = false;
//                }
//            }
//        });
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }

    private void bindCard() {
        String cardNum;
        String num = mEditCDNum.getText().toString();
        cardNum = num.trim();
        String cardMobile = mEditCDMobile.getText().toString();
        String cardName = mEditName.getText().toString();
        if (CommonUtil.isEmpty(cardNum)) {
            showToastShort("卡号不能为空");
            return;
        }
        if (CommonUtil.isEmpty(cardMobile)) {
            showToastShort("手机号码不能为空");
            return;
        }
        if (CommonUtil.isEmpty(cardName)) {
            showToastShort("请选择银行名称");
            return;
        }
        showProgress("处理中");
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.addCreditCard);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("card_code", cardNum);
        param.add("bank_name", cardName);
        param.add("mobile", cardMobile);
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissProgress();
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissProgress();
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 1) {
                        showToastShort("绑定成功");
                        startActivity(CreditCardActivity.class, true, true);
                    } else {
                        showToastShort(responseResult.getInfo());
                    }
                }
            }
        });
    }
}
